﻿#include <Windows.h>
#include <stdio.h>
#include "pentangle.h"

#pragma warning(disable:28159)

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd)
{
	wchar_t wszTitle[] = L"Basic Drawing";

	WNDCLASS wcls;
	ZeroMemory(&wcls, sizeof(WNDCLASS));
	wcls.style = CS_HREDRAW | CS_VREDRAW;
	wcls.lpfnWndProc = WindowProc;
	wcls.hInstance = hInstance;
	wcls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcls.hIcon = LoadIcon(nullptr, IDI_SHIELD);
	wcls.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wcls.lpszClassName = wszTitle;


	if (!RegisterClass(&wcls)) {
		MessageBox(nullptr, L"Register class failed", L"Error", MB_OK);
		return GetLastError();
	}

	HWND hWndMain = CreateWindow(wszTitle, wszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 600, nullptr, nullptr, hInstance, nullptr);
	if (!hWndMain) {
		MessageBox(nullptr, L"Create window failed", L"Error", MB_OK);
		return GetLastError();
	}

	MSG msg;
	while (GetMessage(&msg, nullptr, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HPEN hPen = nullptr, hPen2 = nullptr;
	static HBRUSH hBrush = nullptr;

	switch (uMsg) {
	case WM_CREATE:
	{
		hPen = CreatePen(PS_DOT, 1, RGB(0xff, 0, 0));
		hPen2 = CreatePen(PS_SOLID, 2, RGB(0xff, 0xcc, 0x44));
		hBrush = CreateSolidBrush(RGB(0xff, 0xee, 0xee));

		SetTimer(hWnd, 1, 100, nullptr);

		ShowWindow(hWnd, SW_SHOW);
		return 0;
	}
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hWnd, &ps);
		RECT rc;
		GetClientRect(hWnd, &rc);

		// 坐标模式
		SetMapMode(hdc, MM_ANISOTROPIC);
		SetViewportOrgEx(hdc, rc.right / 2, rc.bottom / 2, nullptr);
		SetWindowExtEx(hdc, 1, -1, nullptr);

		// Pen & Brush
		HGDIOBJ hOldPen = SelectObject(hdc, hPen);
		HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);

		Ellipse(hdc, -200, 200, 200, -200);
		// x
		MoveToEx(hdc, -(rc.right / 2 - 50), 0, nullptr);
		LineTo(hdc, rc.right / 2 - 50, 0);
		// y
		MoveToEx(hdc, 0, -(rc.bottom / 2 - 50), nullptr);
		LineTo(hdc, 0, rc.bottom / 2 - 50);

		SetTextAlign(hdc, TA_CENTER);
		SetBkMode(hdc, TRANSPARENT);
		TextOut(hdc, 0, 0, L"O", 1);
		TextOut(hdc, rc.right / 2 - 50, 0, L"x+", 2);
		SetTextAlign(hdc, TA_LEFT);
		TextOut(hdc, 4, rc.bottom / 2 - 50, L"y+", 2);

		// 毫秒
		DWORD dwCount = GetTickCount();
		SelectObject(hdc, hPen2);
		Pentangle(hdc, 0, 0, 200, dwCount * 3.14f / 30000.0f);
		Pentangle(hdc, 0, 0, 50, dwCount * 3.14f / 5000.0f);

		EndPaint(hWnd, &ps);
		return 0;
	}
	case WM_TIMER:
	{
		InvalidateRect(hWnd, nullptr, FALSE);
		return 0;
	}
	case WM_DESTROY:
	{
		KillTimer(hWnd, 1);

		DeleteObject(hPen);
		DeleteObject(hBrush);

		PostQuitMessage(0);
		return 0;
	}
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}